const {myNotifier} = require('./MyNotifier');
const {getAllActiveBugs, getNewBugs} = require('../data/queryData');
const {getLastBugStatusInStorage, setLastBugStatusInStorage} = require('../data/storage');
const {zentao} = require('../notifier.config.js');
const {compareTime, formatTime} = require('../utils/utils');

let lastBugStatus = null;

function setLastBugStatus (status = formatTime()) {
    lastBugStatus = status;
    setLastBugStatusInStorage(status);
}

function notifierNewBug(res) {
    for (const item of res) {
        let title = item.assignedTo === zentao.account ? `${item.name}有直接指派给你的BUG` : `${item.name}-新BUG-${item.assignedTo}`;
        //间隔发送消息
        setTimeout(() => {
            myNotifier({
                title: title,
                message: item.title,
            }, `${zentao.hostname}/zentaopms/www/index.php?m=bug&f=view&bugID=${item.id}`);

        }, 5000);
    }
}

function getNewBugsByTimeout(){
    setTimeout(() => {
        getNewBugs(lastBugStatus).then(res => {
            if(res.length > 0){
                notifierNewBug(res);
                console.log('66666，有新的BUG待查看！');
            }else{
                console.log('恭喜你，未查询到新增的BUG！');
            }
            setLastBugStatus();
            getNewBugsByTimeout();
        })
    }, zentao.timeout);
}

function notifierStart() {
    getLastBugStatusInStorage().then(res => {
        //缓存时间
        lastBugStatus = res;

        getAllActiveBugs().then(results => {

            //直接指派给自己的bug
            let myBugs = results.filter(v => v.assignedTo === zentao.account);

            //新增的BUG
            let newBugs = results.filter(v => compareTime(v.openedDate, lastBugStatus));

            //根据项目统计数据
            let products = {};
            for (const item of results) {
                if(products[item.product]){
                    products[item.product].count++;
                }else{
                    products[item.product] = {
                        count: 1,
                        name: item.name,
                    };
                }
            }
            let str = '', bugMoreProductName = '', bugMoreCount = -1, productid;
            for (const product in products) {
                const {count, name} = products[product];
                if(count > bugMoreCount){
                    bugMoreProductName = name;
                    bugMoreCount = count;
                    productid = product;
                }
                str += `${name}剩余${count}个，`;
            }

            myNotifier({
                title: '项目BUG统计',
                message: `您关注的项目剩余${results.length}个BUG，其中${str}直接指派给你的BUG有${myBugs.length}个，新增BUG数量${newBugs.length}个`,
            }, `${zentao.hostname}/zentaopms/www/index.php?m=bug&f=browse&productid=${productid}&branch=0&browseType=unresolved&param=0`);

            notifierNewBug(newBugs);

            //缓存当前时间
            setLastBugStatus();

            //开启循环 查询数据库检查新增BUG
            getNewBugsByTimeout();
        });
    })
}



module.exports = {
    notifierStart,
}
